package com.wz.project.stock.stockForzen.service;

import com.wz.common.utils.text.Convert;
import com.wz.project.stock.domain.BasStock;
import com.wz.project.stock.domain.BasStockFrozenRecord;
import com.wz.project.stock.stock.mapper.BasStockMapper;
import com.wz.project.stock.stockForzen.mapper.ForzenMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author jinhuijiang
 * @create 2021-10-27 9:29
 */
@Service
public class ForzenServiceImpl implements ForzenService {

    @Autowired
    ForzenMapper forzenMapper;

    @Autowired
    BasStockMapper stockMapper;

    @Override
    public List<BasStock> findStockList(BasStock basStock) {
        return stockMapper.findStockList(basStock);
    }

    @Override
    public int freezeInStock(String ids,String userName) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String[] stockIds = Convert.toStrArray(ids);
        //获取一个新的库存id

        for (String stockId : stockIds){
            String s = String.valueOf(System.currentTimeMillis());
            String substring = s.substring(s.length() - 6);
            BasStock basStock = stockMapper.findStockListOne(stockId);
            basStock.setBackDate(sdf.format(new Date()));
            basStock.setStockStatus("1");
            basStock.setStockId(substring);
            basStock.setUserId(userName);
            forzenMapper.insertIntoRecord(basStock);
        }
        return this.stockMapper.freezeInStock(stockIds);
    }

    @Override
    public int unfreezeStock(String ids,String userName) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String[] stockIds = Convert.toStrArray(ids);
        //获取新的库存id

        for (String stockId : stockIds){
            String s = String.valueOf(System.currentTimeMillis());
            String substring = s.substring(s.length() - 6);
            BasStock basStock = stockMapper.findStockListOne(stockId);
            basStock.setBackDate(sdf.format(new Date()));
            basStock.setStockStatus("0");
            basStock.setStockId(substring);
            basStock.setUserId(userName);
            forzenMapper.insertIntoRecord(basStock);
        }
        return this.stockMapper.unfreezeStock(stockIds);
    }

    @Override
    public List<BasStockFrozenRecord> findFrozenList(BasStockFrozenRecord record) {
        return forzenMapper.findFeozenList(record);
    }
}
